package net.zjitc.service.impl;

import net.zjitc.mapper.BookMapper;
import net.zjitc.mapper.BookOrderHistoryMapper;
import net.zjitc.mapper.NoticeMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.pojo.api.ApiResult;
import net.zjitc.pojo.api.Result;
import net.zjitc.pojo.dto.query.extend.BookOrderHistoryQueryDto;
import net.zjitc.pojo.dto.query.extend.BookQueryDto;
import net.zjitc.pojo.dto.query.extend.NoticeQueryDto;
import net.zjitc.pojo.dto.query.extend.UserQueryDto;
import net.zjitc.pojo.vo.ChartVO;
import net.zjitc.service.ViewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//首页可视化
@Service
public class ViewsServiceImpl implements ViewsService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private BookOrderHistoryMapper bookOrderHistoryMapper;

    //统计系统的基础数据
    @Override
    public Result<List<ChartVO>> staticControls() {
        List<ChartVO> chartVOS = new ArrayList<>();
        UserQueryDto userQueryDto = new UserQueryDto();
        int userCount = userMapper.queryCount(userQueryDto);
        change(userCount, "存量用户（个）", chartVOS);
        NoticeQueryDto noticeQueryDto = new NoticeQueryDto();
        int noticeCount = noticeMapper.queryCount(noticeQueryDto);
        change(noticeCount, "公告（篇）", chartVOS);
        BookQueryDto bookQueryDto = new BookQueryDto();
        int bookCount = bookMapper.queryCount(bookQueryDto);
        change(bookCount, "收录图书", chartVOS);
        BookOrderHistoryQueryDto bookOrderHistoryQueryDto = new BookOrderHistoryQueryDto();
        int bookOrderHistoryCount = bookOrderHistoryMapper.queryCount(bookOrderHistoryQueryDto);
        change(bookOrderHistoryCount, "借阅记录", chartVOS);
        return ApiResult.success(chartVOS);
    }

    //参数处理
    private void change(Integer count, String name, List<ChartVO> chartVOS) {
        ChartVO chartVO = new ChartVO(name, count);
        chartVOS.add(chartVO);
    }

}